﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AusProject.OrderComponents
{
    public partial class ComponentRow : UserControl
    {
        private int numOfCol;

        public int NumOfCol
        {
            get { return numOfCol; }
            set {
                if (value > 0)
                {
                    if (value != numOfCol)
                    {
                        numOfCol = value;

                        cells = new ComponentCell[numOfCol];
                        for (int i = 0; i < numOfCol; i++)
                        {
                            cells[i] = new ComponentCell(true, 1.0 / numOfCol);
                        }
                        CreateCells();
                    }
                }
            }
        }


        private ComponentCell[] cells;

        public ComponentCell[] Cells
        {
            get { return cells; }
            set
            {
                if ((value != null) && (value.Length > 0))
                {
                    cells = value;

                    numOfCol = cells.Length;

                    double sum = 0;
                    foreach (ComponentCell col in cells)
                    {
                        sum += col.WidthPercent;
                    }
                    if (sum != 1.0)
                    {
                        for (int i = 0; i < numOfCol; i++)
                        {
                            cells[i].WidthPercent = cells[i].WidthPercent / sum;
                        }
                    }
                    CreateCells();
                }
            }
        }


        private bool isFirstLine;

        public bool IsFirstLine
        {
            get { return isFirstLine; }
            set
            {
                isFirstLine = value;
                this.Invalidate();
            }
        }

        private bool isLastLine;

        public bool IsLastLine
        {
            get { return isLastLine; }
            set {
                isLastLine = value;
                this.Invalidate();
            }
        }

        private void CreateCells()
        {
            this.Controls.Clear();
            int prevX = 0;
            for (int i = 0; i < numOfCol; i++)
            {
                Label cell = new Label();
                cell.Left = prevX + 1;
                cell.Top = 1;
                cell.AutoSize = false;
                cell.Height = this.Height - 2;
                if (i < numOfCol - 1)
                {
                    cell.Width = (int)Math.Round((this.Width - numOfCol - 1) * cells[i].WidthPercent);
                }
                else
                {
                    cell.Width = this.Width - prevX - 2;
                }
                cell.TextAlign = ContentAlignment.MiddleLeft;
                cell.Text = "";
                cell.MouseClick += new MouseEventHandler(this.label_MouseClick);
                this.Controls.Add(cell);
                prevX = cell.Left + cell.Width;
            }
            this.Invalidate();
        }


        public object Data { get; set; }


        protected Pen borderPen;
        protected Pen borderDashPen;

        public ComponentRow()
        {
            InitializeComponent();
            borderPen = new Pen(new SolidBrush(Color.Black), 1);
            borderDashPen = new Pen(new SolidBrush(Color.Black), 1);
            borderDashPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

            NumOfCol = 1;
            Data = null;
        }

        private void ComponentLine_Paint(object sender, PaintEventArgs e)
        {
            // Top Edge
            if (isFirstLine)
            {
                e.Graphics.DrawLine(borderPen, 0, 0, this.Width - 1, 0);
            }
            else
            {
                e.Graphics.DrawLine(borderDashPen, 0, 0, this.Width - 1, 0);
            }

            // Bottom Edge
            if (isLastLine)
            {
                e.Graphics.DrawLine(borderPen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
            }
            else
            {
                e.Graphics.DrawLine(borderDashPen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
            }

            // Left Edge
            e.Graphics.DrawLine(borderPen, 0, 0, 0, this.Height - 1);

            // Right Edge
            e.Graphics.DrawLine(borderPen, this.Width - 1, 0, this.Width - 1, this.Height - 1);

            for (int i = 0; i < cells.Length; i++)
            {
                if (cells[i].ShowBorder)
                {
                    e.Graphics.DrawLine(borderPen, this.Controls[i].Left - 1, 0, this.Controls[i].Left - 1, this.Height - 1);
                    e.Graphics.DrawLine(borderPen, this.Controls[i].Left + this.Controls[i].Width, 0, this.Controls[i].Left + this.Controls[i].Width, this.Height - 1);
                }
            }
        }

        private void ComponentLine_Resize(object sender, EventArgs e)
        {
            int prevX = 0;

            for (int i = 0; i < this.Controls.Count; i++)
            {
                this.Controls[i].Left = prevX + 1;
                if (i < numOfCol - 1)
                {
                    this.Controls[i].Width = (int)Math.Round((this.Width - numOfCol - 1) * cells[i].WidthPercent);
                }
                else
                {
                    this.Controls[i].Width = this.Width - prevX - 2;
                }
                this.Controls[i].Height = this.Height - 2;
                prevX = this.Controls[i].Left + this.Controls[i].Width;
            }
        }

        public void SetTextOfCol(int colIndex, string text)
        {
            if ((colIndex >= 0) && (colIndex < numOfCol))
            {
                this.Controls[colIndex].Text = text;
            }
        }

        public string GetTextOfCol(int colIndex)
        {
            if ((colIndex >= 0) && (colIndex < numOfCol))
            {
                return Controls[colIndex].Text;
            }
            else
            {
                return null;
            }
        }

        private void label_MouseClick(object sender, MouseEventArgs e)
        {
            Point p = this.PointToClient(((Control)sender).PointToScreen(e.Location));
            this.OnMouseClick(new MouseEventArgs(e.Button, e.Clicks, p.X, p.Y, e.Delta));
        }

        public void Highlight()
        {
            this.BackColor = SystemColors.Highlight;
        }

        public void Unhighlight()
        {
            this.BackColor = Color.Transparent;
        }
    }
}
